<?php
namespace app\job;

use app\common\model\Agent;
use app\common\model\AgentMoneyRecord;
use app\common\model\AgentWithdrawalRecord;
use app\common\model\Member;
use app\common\model\MoneyRecord;
use app\common\model\PlatformMoneyRecord;
use think\Db;
use think\Exception;
use think\Log;
use think\Queue;
use think\queue\Job;
use Wxcrypt\XcxPay;

class MemberWxPay extends BaseJob
{
    /**
     * 用户余额提现查询
     * @param Job $job
     * @param $data
     */
    public function handle(Job $job, $data)
    {
        Db::startTrans();//开启事务
        try{
            // 有些消息在到达消费者时,可能已经不再需要执行了
            $info = MoneyRecord::get(['id'=>$data['id']]);
            if (!empty($info)){
                if ($info->detail_type == 2 && $info->tx_status == 1){ //提现类别 并且处于 转账中状态
                    $xccPay = new XcxPay();
                    $result = $xccPay->detailsQuery($info->out_batch_no,$info->out_batch_no);
                    if (isset($result['code']))
                        throw new Exception('申请微信支付失败：'.$result['message']);
                    if (isset($result['fail_reason'])){//转账失败
                        $member = Member::get($info->member_id);
                        //退回余额
                        $balance = bcadd($member->balance,$info->money,2);
                        $member->balance = $balance;
                        $member->save();
                        //退回记录
                        $res = MoneyRecord::create(['type'=>MoneyRecord::TYPE_INCOME,'detail_type'=>MoneyRecord::DETAIL_TYPE_TXREFUND,'money'=>$info->money,'balance'=>$member->balance,'member_id'=>$info->member_id,'tx_status'=>0,'remark'=>'提现失败，退回到余额']);
                        $info->tx_status = 3;//设为失败
                        $info->detail_status = $result['detail_status'];//记录状态
                        $info->fail_reason = $result['fail_reason'];//记录失败原因
                        $info->remark = '提现失败';//记录失败原因
                        $info->save();
                        Queue::push(\app\job\Message::class,[
                            'member_id'=>$info['member_id'],
                            'template_id'=>config('wechat_gzh')['gzh_template']['tixian_audit'],
                            'data'=>[
                                'time1'=>date('Y-m-d'),
                                'amount2'=>$info->money,
                                'thing3'=>'提现审核失败'
                            ]
                        ],'message');
                    }else{//转账成功
                        $info->tx_status = 2;
                        $info->remark = '提现完成';
                        $info->save();
                        Queue::push(\app\job\Message::class,[
                            'member_id'=>$info->member_id,
                            'template_id'=>config('wechat_gzh')['gzh_template']['tixian_success'],
                            'data'=>[
                                'time3'=>$info['createtime'],
                                'character_string2'=>$info['id'],
                                'thing4'=>'微信零钱' ,
                                'amount5'=>$info['money'],
                                'thing7'=>"本人"
                            ]
                        ],'message');
                    }
                }
            }
            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            Log::error($e->getMessage());
            throw $e;
        }
    }









}